<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vue入门指南——计算属性</title>
    <link rel="stylesheet" href="common.css" />
    <script type="importmap">
      {
        "imports": {
          "vue": "https://lib.baomitu.com/vue/latest/vue.esm-browser.js"
        }
      }
    </script>
  </head>
  <body>
    <div id="app">
      <p>{{ foo()}}</p>
      <p>方法与计算属性不同在于，后者会进行响应式依赖缓存</p>
      <p>{{publishedBooksMessage}}</p>
    </div>
    <script type="module">
      import { createApp } from "vue";

      createApp({
        data() {
          return {
            author: {
              name: "张三",
              books: ["书籍1", "书籍2", "书籍3"],
            },
          };
        },

        methods: {
          foo() {
            return this.author.books.length > 0 ? "已出版" : "未出版";
          },
        },

        //计算属性
        computed: {
          publishedBooksMessage() {
            return this.author.books.length > 0 ? "已出版" : "未出版";
          },
        },
      }).mount("#app");
    </script>
  </body>
</html>
